package org.zhxy.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhxy.zhxy.dao.AppointRecordDao;
import org.zhxy.zhxy.dao.ClassroomDao;
import org.zhxy.zhxy.entity.AppointRecord;
import org.zhxy.zhxy.entity.Classroom;
import org.zhxy.zhxy.entity.dto.PageDto;
import org.zhxy.zhxy.service.ClassroomService;
import org.zhxy.zhxy.utils.PageResult;
import org.zhxy.zhxy.utils.Result;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClassroomServiceImpl extends ServiceImpl<ClassroomDao, Classroom> implements ClassroomService {

    @Autowired
    private ClassroomDao classroomDao;

    @Resource
    private AppointRecordDao appointRecordDao;

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Result listClassroom(PageDto pageDto) {
        Integer page = pageDto.getPage();
        Integer pageSize = pageDto.getPageSize();
        if (page == null) {
            return Result.fail("页数不能为空");
        }
        if (pageSize == null) {
            return Result.fail("页大小不能为空");
        }

        String keyword = pageDto.getKeyword();
        QueryWrapper<Classroom> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword)) {
            if(StrUtil.isNumeric(keyword)){
                queryWrapper.like("name", keyword);
            }else{
                queryWrapper.like("building", keyword);
            }
        }
        Page<Classroom> pageQuery = new Page<>(page, pageSize);
        classroomDao.selectPage(pageQuery, queryWrapper);

        List<Classroom> records = pageQuery.getRecords();
        long total = pageQuery.getTotal();
        PageResult<Classroom> pageResult = new PageResult<>(total, records);
        return Result.success(pageResult);
    }


    @Override
    public List<String> selectBuilding() {
        QueryWrapper<Classroom> classroomQueryWrapper = new QueryWrapper<>();
        classroomQueryWrapper.select("distinct building");
        return classroomDao.selectList(classroomQueryWrapper).stream().map(Classroom::getBuilding).collect(Collectors.toList());
    }

    @Override
    public List<Classroom> getClassroom(String building) {
        QueryWrapper<Classroom> classroomQueryWrapper = new QueryWrapper<>();
        classroomQueryWrapper.eq("building",building);

        List<Classroom> classrooms =null;
      try {
          classrooms =  classroomDao.selectList(classroomQueryWrapper);
      }catch (Exception e){
          e.printStackTrace();
      }
      return classrooms;
    }

    @Override
    public List<String> selectNotAppontTime(Integer roomId) {
        QueryWrapper<AppointRecord> appointRecordQueryWrapper = new QueryWrapper<>();
        appointRecordQueryWrapper.eq("room_id",roomId);
        appointRecordQueryWrapper.eq("status",1);
        Date date = new Date();
        long time = date.getTime();
        appointRecordQueryWrapper.gt("end_time",time);
        List<AppointRecord> appointRecords = appointRecordDao.selectList(appointRecordQueryWrapper);
        List<String> res = new ArrayList<>(appointRecords.size());
        for (AppointRecord appointRecord : appointRecords){
            Long startTime = appointRecord.getStartTime();
            Long endTime = appointRecord.getEndTime();
            Date date1 = new Date(startTime);
            String format = sdf.format(date1);
            Date date2 = new Date(endTime);
            String format2 = sdf.format(date2);
            res.add(format.substring(5)+"  -  "+format2.substring(5));
        }
        return res;

    }
}
